﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.DataSourceData;
using WoSoft.PACS.Data.Models;
using DevComponents.DotNetBar;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmDataSource : FrmBase
    {
        /// <summary>
        /// 获取选择的数据源
        /// </summary>
        public WoSoft.PACS.Data.Models.DataSourceModel SelectDataSource { get; private set; }

        DataSourceService dss = new DataSourceService();

        public FrmDataSource(DataSourceModel model)
        {
            InitializeComponent();

            SelectDataSource = model;
        }


        #region 工具栏维护
        //add
        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmDataSourceConfig dsConfig = new FrmDataSourceConfig(0);
            var r = dsConfig.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                RefreshData();
            }
        }

        //编辑 
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count > 0)
            {
                var id = this.dataGridViewX1.SelectedRows[0].Cells["columnID"].Value;
                FrmDataSourceConfig dsConfig = new FrmDataSourceConfig(Convert.ToInt32(id));
                var r = dsConfig.ShowDialog(this);
                if (r == System.Windows.Forms.DialogResult.OK)
                {
                    RefreshData();
                }
            }
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.SelectedRows.Count <= 0)
            {
                return;
            }

            var id = this.dataGridViewX1.SelectedRows[0].Cells["columnID"].Value.ToString();
            if (!string.IsNullOrEmpty(id))
            {
                //确定要删除
                if (MessageBoxEx.Show("您确定要删除该数据源吗？", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    dss.DeleteData(id);
                    RefreshData();
                }
            }
        }

        //选择数据源
        private void btnSelected_Click(object sender, EventArgs e)
        {
            //if (this.dataGridViewX1.SelectedRows.Count > 0)
            //{
            //    var id = this.dataGridViewX1.SelectedRows[0].Cells["columnID"].Value.ToString();
            //    var model = dss.GetModelByID(Convert.ToInt32(id));
            //    if (model != null)
            //    {
            //        this.SelectDataSource = model;
            //        this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //        this.Close();
            //    }
            //}
            this.Close();
        }


        #endregion

        #region 加载数据
        //加载
        private void FrmDataSource_Load(object sender, EventArgs e)
        {
            RefreshData();

            this.dataGridViewX1.ClearSelection();
            //如果传入值不为空(显示当前数据默认源)
            if (SelectDataSource != null)
            {
                int cnt = this.dataGridViewX1.Rows.Count;
                for (int i = 0; i < cnt; i++)
                {
                    if (Convert.ToInt32(this.dataGridViewX1.Rows[i].Cells["columnID"].Value) == SelectDataSource.ID)
                    {
                        this.dataGridViewX1.Rows[i].Selected = true;
                        return;
                    }
                }
            }
        }

        //刷新数据
        private void RefreshData()
        {
            //获取所有数据源
            var dt = dss.GetAllData();

            this.dataGridViewX1.DataSource = dt;
        }
        #endregion

    }
}
